$(document).ready(resetHeight(1));
function resetHeight(code) {
    var h1;
    if (code == 1)
        h1 = $(".tab-pane.active").height() + 160;
    else if (code == 2)
        h1 = $("#divEdit").height();
    $('#mainFrame', parent.document).height(Math.max(h1, 650));
    $('.FlowContainer').height(Math.max(h1, 650));
}

function exportAccountSubject() {
    if (confirm("是否导出?")) return;

}
function injectExcel() {
    if (confirm("是否导入?")) return;

}

$("img[title=编辑]").click(function () {
    $("#divMain").hide();
    $("#divEdit").animate({left: "0px"});
    resetButton();
    $("#btnAdd").hide();
    $("#btnSave1").hide();
    initEditDivContent1(this);


    resetHeight(2);
});
$("img[title=新建]").click(function () {
    $("#divMain").hide();
    $("#divEdit").animate({left: "0px"});
    resetButton();
    $("#btnSave2").hide();
    initEditDivContent2(this);


    resetHeight(2);
});
function resetButton() {
    $("#btnAdd").show();
    $("#btnSave1").show();
    $("#btnSave2").show();
}
function setSubjectType(type) {
    $("#edit_subject_type").text(type);
    $("#edit_subject_type").attr("title", type);
}
function initEditDivContent1(_this) {
    var subjectType = $("li[id^=tab-nav-li][class=active] a").text();
    setSubjectType(subjectType + "类");
    var tds = $(_this).parents("tr").find("td");
    var subject_id = $(tds[0]).text().trim();
    var subject_name = $(tds[1]).text().trim();
    var subject_help_code = $(tds[2]).text().trim();
    var subject_direction = $(tds[3]).text().trim();
    var subject_state = $(tds[4]).text().trim();
    $("#edit_subject_id").val(" " + subject_id);
    $("#edit_subject_name").val(subject_name);
    if (subject_id.length > 4) {
        var parent_subject_id = subject_id.substr(0, 4);
        var parent_subject_name = $($("#" + parent_subject_id).find("td")[1]).text();
        $("#edit_parent_subject_name").val(parent_subject_name);
    } else {
        $("#edit_parent_subject_name").val("");
    }
    if (subject_direction == "借") {
        $("#rdJie")[0].checked = true;
        $("#rdDai")[0].checked = false;
    } else {
        $("#rdJie")[0].checked = false;
        $("#rdDai")[0].checked = true;
    }
    if (subject_state == "正常") {
        $("#rdStatus0")[0].checked = true;
        $("#rdStatus1")[0].checked = false;
    } else {
        $("#rdStatus1")[0].checked = true;
        $("#rdStatus0")[0].checked = false;
    }
}

function initEditDivContent2(_this) {
    var subjectType = $("li[id^=tab-nav-li][class=active] a").text();
    setSubjectType(subjectType + "类");
    var tds = $(_this).parents("tr").find("td");
    var subject_id = $(tds[0]).text().trim();
    var subject_name = $(tds[1]).text().trim();
    var subject_help_code = $(tds[2]).text().trim();
    var subject_direction = $(tds[3]).text().trim();
    var subject_state = $(tds[4]).text().trim();
    $("#edit_subject_id").val(" " + subject_id+"001");
    $("#edit_subject_name").val("");
    $("#edit_parent_subject_name").val(subject_name);
    if (subject_direction == "借") {
        $("#rdJie")[0].checked = true;
        $("#rdDai")[0].checked = false;
    } else {
        $("#rdJie")[0].checked = false;
        $("#rdDai")[0].checked = true;
    }
    $("#rdStatus0")[0].checked = false;
    $("#rdStatus1")[0].checked = true;
}


function Cancel() {
    $("#divEdit").animate({left: "1000px"});
    $("#divMain").show();
    resetHeight(1);
    setTimeout(function () {
        document.location.reload();
    }, 300);
}
//保存并退出
function SubmitSave() {
    saveAccountSubject(function(){
        Cancel();
    });
}
//保存并新增
function SubmitAndAdd() {
    saveAccountSubject(function(){});
    //清空数据
}
function SubmitUpdate() {
    updateAccountSubject(function(){
        Cancel();
    });
}


//保存会计科目
function saveAccountSubject(successCallBack) {
    var url = "../accountSubject/insert";
    var data = gainTableDate();
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function (data) {
            if (data.success) {
                successCallBack();
            }
            window.parent.showMessage(data.message);
        }
    });
}
function updateAccountSubject(successCallBack) {
    var url = "../accountSubject/update";
    var data = gainTableDate();
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function (data) {
            if (data.success) {
                successCallBack();
            }
            window.parent.showMessage(data.message);
        }
    });
}

function gainTableDate() {
    var data = {};
    data.subject_id = $("#edit_subject_id").val().trim();
    data.subject_name = $("#edit_subject_name").val();
    data.subject_type = $("#edit_subject_type").attr("title").substr(0,$("#edit_subject_type").attr("title").length-1);
    data.subject_direction = $("#rdJie")[0].checked == true ? "借" : "贷";
    data.subject_state = $("#rdStatus0")[0].checked == true ? 1 : 0;
    return data;
}


$("li[id^=tab-nav-li]").click(function () {
    $("#nav-tabs").find("li").removeClass("active");
    $("#tab-content>div").removeClass("active");
    $(this).addClass("active");
    var index = $(this).attr("id").match(/tab-nav-li-([0-9]+)/)[1];
    $("#tab-" + index).addClass("active");
    resetHeight(1);
});














